const express = require('express');
const router = express.Router();

router.get('/uuid', (req, res) => {
    // get client ip address
    let ipAddress = req.ip;
    console.log('ip: ', ipAddress);
    if (ipAddress === '::1') {
        // using localhost
        ipAddress = '127.0.0.1';
    } else {
        const ipv4Regex = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
        const match = ipv4Regex.exec(ipAddress);
        ipAddress = match[1];
    }
    if (!ipAddress) {
        res.send('ip address error');
        return;
    }
    console.log('ipv4: ', ipAddress);
    res.json({ uuid: ipAddress + '_' + Date.now() });
});

module.exports = router;
